<?php

class Event extends CI_Model
{

    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->load->database();
        date_default_timezone_set('PRC');
    }

    /**
     * 添加订单
     *
     * @param $data
     * @return mixed
     */
    public function insert($data)
    {
        return $this->db->insert('event_order', $data);
    }


    /**
     * 获取所有信息
     *
     * @return mixed
     */
    function  get_all_by_event($data)
    {
        $this->db->where($data);
        return $this->db->count_all_results('event_order');
    }

    /**
     * 获取所有信息
     *
     * @param $open_id
     * @return mixed
     */
    public function get_events_by($data,$limit="", $start=0)
    {
        $this->db->from('event_order');
        $this->db->where($data);
        if(!empty($limit) )
        {
            $this->db->limit($limit, $start);
        }
        $this->db->order_by('is_pay desc, trade_status asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 发货
     *
     * @param $id
     */
    public function update_event($id){
        $this->db->query('update event_order set status = 2 where id ='.$id);
    }

    /**
     * 支付
     * @param $id
     */
    public function update_pay($code,$data){

        $this->db->where('code', $code);
        return $this->db->update('event_order', $data);
//          $this->db->query('update event_order set is_pay = 2 where code ='.$code);
    }



    /**
     * 获取中奖用户
     *
     * @param $limit
     * @param $start
     * @return mixed
     */
    function get_prize_user($limit, $start)
    {
        $sql = 'select * from prize_user  ORDER BY id DESC limit '.$start.','.$limit;
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    /**
     * 更新user
     *
     * @param $data
     */
    function update_user($data)
    {

        $this->db->update('thinkcms_user', $data);
    }


    /**
     * 根据微信id获取已参的团
     *
     * @param $open_id
     * @return mixed
     */
        public function get_tuan($data,$params = 'tuan.*', $start = null,$limit = null )
        {
            $this->db->select($params);
            $this->db->from('tuan');
            $this->db->join('weixin_mobile', 'weixin_mobile.open_id = tuan.open_id', 'left');
            $this->db->where($data);
            if($limit !== null && $start !== null){
                $this->db->limit($limit, $start);
            }
            $query = $this->db->get();
            return $query->result_array();
        }

    /**
     * 获取团成员
     *
     * @param $open_id
     * @return mixed
     */
    public function get_tuan_member($data, $params = 'tuan.*')
    {
        $this->db->select($params);
        $this->db->from('tuan');
        $this->db->join('tuanyuan', 'tuan.id = tuanyuan.tuan_id', 'left');
        $this->db->join('weixin_mobile', 'weixin_mobile.open_id = tuanyuan.open_id', 'left');
        $this->db->where($data);
        $this->db->order_by('tuanyuan.create_time','asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取用户手机号
     *
     * @param $open_id
     * @return mixed
     */
    public function get_weixin_mobile($data)
    {
        $query = $this->db->get_where('event_order', $data);
        return $query->row_array();
    }

    /**
     * 添加团
     *
     * @param $data
     * @return mixed
     */
    public function insert_tuan($data)
    {
        $this->db->insert('tuan',$data);
        return $this->db->insert_id();
    }

    /**
     * 添加团员
     *
     * @param $data
     * @return mixed
     */
    public function insert_tuanyuan($tuan_id,$data,$empty_user = false, $user_data = null)
    {
        $this->db->trans_start();
        $query = $this->db->get_where('tuanyuan', array('tuan_id' => $tuan_id));
        $count = $query->result_array();
        if(count($count) == 9){
            $this->db->trans_rollback();
            return false;
        }
        $this->db->insert('tuanyuan',$data);
        //满团
        if(count($count) == 7){
            $this->db->where(array('id' => $tuan_id));
            $this->db->update('tuan', array('status' => 1));
        }
        if($empty_user){
            $this->insert_m_wx($user_data);
        }
        $this->db->trans_complete();
        return true;
    }



    /**
     * 获取用户手机号
     *
     * @param $open_id
     * @return mixed
     */
    public function get_tuanyuans($data)
    {
        $query = $this->db->get_where('tuanyuan', $data);
        return $query->result_array();
    }
}